import { useCheckType } from "./util"

const { check } = useCheckType()

interface formRuleResponse {
	msg:string
	flag:boolean
}

type formRule = (val:any)=>formRuleResponse


//验证数据不为空
export const notEmpty = ():formRule=>{
	return (val)=>{
		return {
			msg:`不能为空`,
			flag:Boolean(Array.isArray(val)?val.length:val)
		}
	}
}


//验证字符串长度
export const strLen = (max:number,min:number):formRule=>{
	return (val)=>{
		let msg = ''
		let flag = true
		max = +max || 100
		min = +min || 1
		if(val.length>max || val.length<min){
			msg = `只能是${min}到${max}个字符之内`
			flag = false
		}
		return {
			msg,
			flag
		}
	}
}


//验证手机号格式
export const phoneCheck = ():formRule=>{
	return (val)=>{
		return {
			msg:`格式错误`,
			flag:check(val,'phone')
		}
	}
}


//验证身份证格式
export const idCardCheck = ():formRule=>{
	return (val)=>{
		return {
			msg:`格式错误`,
			flag:check(val,'id')
		}
	}
}

//验证邮箱格式
export const emailCheck = ():formRule=>{
	return (val)=>{
		return {
			msg:`格式错误`,
			flag:check(val,'email')
		}
	}
}